<template>
  <div class="content">
    <el-button type="primary" @click="show = true" v-if="$_permissions() == '2' || $_permissions() == '3'">新增</el-button>
    <div class="table">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" width="150" label="ID" align="center"></el-table-column>
        <el-table-column prop="pic_src" label="图片" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100%; height: 250px" :src="scope.row.pic_src" :preview-src-list="[scope.row.pic_src]"></el-image>
          </template>
        </el-table-column>
        <el-table-column fixed="right" v-if="$_permissions() == '2' || $_permissions() == '3'" label="操作" width="250" align="center">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row, true)" type="text" size="small">更换图片</el-button>
            <el-button type="text" size="small" @click="handleClick(scope.row, false)" v-if="$_permissions() == '3'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.start"
      :page-sizes="[10, 25, 50, 100]"
      :page-size="pagination.nums"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
    ></el-pagination>
    <el-dialog :title="ruleForm.id ? '编辑' : '添加'" :visible.sync="show" width="30%" @close="handleClose">
      <div>
        <el-form :model="ruleForm" ref="ruleForm" label-width="80px" class="demo-ruleForm">
          <el-form-item label="图片" prop="name">
            <div class="upload_plus">
              <i class="el-icon-plus"></i>
              <img v-show="ruleForm.pic_src" :src="ruleForm.pic_src" />
              <input class="file" accept="image/*" type="file" ref="file" @change="upload" />
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">{{ ruleForm.id ? '保存' : '创建' }}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAlllbPic, updatelbPic, dellbPic, insterlbPic, upload } from '@/api/test.js';
import pagination from '@/mixins/page.js';
export default {
  name: 'hongcaitj',
  mixins: [pagination],
  data() {
    return {
      tableData: [],
      show: false,
      ruleForm: {
        pic_src: ''
      }
    };
  },
  methods: {
    upload() {
      let file = this.$refs['file'].files[0];
      if (file) {
        const loading = this.$loading({
          lock: true,
          text: '图片上传中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.5)'
        });
        let fromdata = new FormData();
        fromdata.append('file', file);
        upload(fromdata).then(res => {
          console.log(res);
          if (res.src) {
            console.log('上传成功');
            this.ruleForm.pic_src = res.src;
            setTimeout(() => {
              loading.close();
            }, 500);
          }
        });
      }
      console.log(file);
    },
    handleClick(row, status) {
      //status true:编辑 false:删除
      console.log(row);
      if (status) {
        //编辑
        this.ruleForm = { ...row };
        this.show = true;
      } else {
        //删除
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            dellbPic({ id: row.id }).then(res => {
              console.log(res);
              if (res.msg.indexOf('成功') != -1) {
                this.$message.success('删除成功');
                this.getList();
              } else {
                this.$message.error('删除失败');
              }
            });
          })
          .catch(() => {});
      }
    },
    getList() {
      //获取列表
      getAlllbPic(this.pagination).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.pagination.total = res.total;
      });
    },
    submitForm() {
      //提交请求
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          if (this.ruleForm.id) {
            //更新
            updatelbPic({
              id: this.ruleForm.id,
              PicSrc: this.ruleForm.pic_src
            }).then(res => {
              if (res.msg.indexOf('成功') != -1) {
                this.$message.success(res.msg);
                this.handleClose();
                this.getList();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            //添加
            insterlbPic({
              PicSrc: this.ruleForm.pic_src
            }).then(res => {
              if (res.msg.indexOf('成功') != -1) {
                this.$message.success(res.msg);
                this.handleClose();
                this.getList();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
    handleClose() {
      //弹窗关闭回调 -- 清空表单数据
      this.ruleForm = {
        pic_src: ''
      };
      this.show = false;
      this.$refs['ruleForm'].resetFields();
    }
  },
  mounted() {
    this.getList();
  }
};
</script>

<style lang="scss" scoped>
.content {
  .upload_plus {
    width: 100%;
    height: 180px;
    overflow: hidden;
    box-shadow: 0 0 5px 1px #ddd;
    border-radius: 4px;
    position: relative;

    &:hover {
      color: #333;
    }
    > i {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      font-size: 35px;
    }
    > img {
      width: 100%;
      height: 100%;
      position: relative;
      z-index: 1;
    }
    .file {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      opacity: 0;
      cursor: pointer;
      z-index: 2;
    }
  }
}
</style>
